package com.example.item01.service.impl;

import com.example.item01.mapper.DeptLogMapper;
import com.example.item01.mapper.DeptMapper;
import com.example.item01.mapper.EmpMapper;
import com.example.item01.proj.Dept;
import com.example.item01.proj.DeptLog;
import com.example.item01.service.DeptLogService;
import com.example.item01.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired

    private EmpMapper empMapper;
    @Autowired
    private DeptLogService deptLogService;

    @Override
    public List<Dept> list() {
        return deptMapper.list2();
    }
    @Transactional
    @Override
    public void delet(Integer id) {
        try {
            deptMapper.delet(id);
            empMapper.deleteByDeptId(id);
        } finally {
            DeptLog deptLog=new DeptLog();
            deptLog.setCreateTime(LocalDateTime.now());
            deptLog.setDescription("执行了解散部门的操作,此次执行的是第"+id+"号部门");
            deptLogService.insert(deptLog);
        }

    }
    @Override
    public void insert(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }

    @Override
    public Dept selectid(Integer id) {
        Dept dept = deptMapper.selectid(id);
        return dept;
    }

    @Override
    public void replace(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.replace(dept);

    }


}
